<template>
  <div class="login_container">
    <div class="login_class">
      <el-form ref="ruleForms" :model="data.loginData" :rules="rules" class="loginFrom" :label-position="right" label-width="65px">
        <el-row>
          <el-form-item label="账户" prop="userAccount">
            <el-input v-model="data.loginData.userAccount" placeholder="请输入账户"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="data.loginData.password" placeholder="请输入密码"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="验证码" prop="checkCode">
            <el-input type="text" v-model="data.loginData.checkCode" style="margin-right: 5px; width: 140px" placeholder="点击图片刷新验证码"> </el-input>
            <div class="hands" @click="updataCkeck"><img v-bind:src="data.checkCode" class="checkCode" /></div>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-checkbox v-model="data.loginData.isRemember">记住密码</el-checkbox>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" @click="login(form)" style="width: 120px"> 登录</el-button>
              <el-button type="primary" @click="login(form)" style="width: 120px"> 注册</el-button>
            </el-button-group>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script setup>
  import { ref, reactive, toRefs, onBeforeMount, getCurrentInstance } from 'vue';
  let { proxy } = getCurrentInstance();

  const checkImg = ref('system/sys-user/checkCode?' + new Date().getTime());
  const updataCkeck = () => {
    data.checkCode = 'system/sys-user/checkCode?' + new Date().getTime();
  };
  const rules = ref({
    userAccount: [{ required: true, message: '请输入账户', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    checkCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
  });
  const data = reactive({
    loginData: { userAccount: '', password: '' },
    checkCode: 'system/sys-user/checkCode?' + new Date().getTime(),
  });
  const login = () => {
    //校验      proxy.$message.warning('必填项不能为空');
    console.log(data.loginData);
    if (data.loginData.userAccount == '') {
      return;
    }
    if (data.loginData.password == '') {
      return;
    }

    proxy.$api.system.userLogin(data.loginData).then(res => {
      debugger;
      if (res.code > 0) {
        console.log(res);
        proxy.$storage.setItem('userInfo', res);
        proxy.$storage.setItem('satoken', res.satoken);
        proxy.$emit('LoginFalsh');
        proxy.$router.replace('/home');
      } else {
        //后端校验出错
        proxy.$message({
          type: 'error',
          message: res.msg,
        });
      }
    });
  };
</script>
<style lang="scss" scoped>
  .login_container {
    background: url('@/assets/img/login.jpg') no-repeat;
    background-size: 100% 100%;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
  .login_class {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: max-content;
  }
  :deep(.el-form-item__label) {
    color: #ffffff;
  }
  :deep(.el-input__wrapper) {
    width: 250px;
  }
  .checkCode {
    width: 125px;
    height: 30px;
    cursor: pointer;
  }
</style>
